package com.scm.servlet.pomain;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.scm.dao.PoitemDao;
import com.scm.dao.PomainDao;
import com.scm.dao.ProductDao;
import com.scm.model.Poitem;
import com.scm.model.Pomain;
import com.scm.util.DbUtil;

/**
 * Servlet implementation class AddPomainServlet
 */
@WebServlet("/main/pomain/AddPomain")
public class AddPomainServlet extends HttpServlet {
	private static final long serialVersionUID = -7253232286586859506L;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String poId = req.getParameter("poId");
		String createTime = req.getParameter("createTime");
		String venderCode = req.getParameter("venderCode");
		String account = req.getParameter("account");
		String tipFee = req.getParameter("tipFee");
		String productTotal = req.getParameter("productTotal");
		String payType = req.getParameter("payType");
			if("货到付款".equals(payType)) {
				payType="1";
			}else if("款到发货".equals(payType)) {
				payType="2";
			}else if("预付款到发货".equals(payType)) {
				payType="3";
			}
		String remark = req.getParameter("remark");
		String prePayFee = req.getParameter("prePayFee");
		String[] productCodes = req.getParameterValues("productCode");
		String[] name = req.getParameterValues("name");
		String[] unitName = req.getParameterValues("unitName");
		String[] num = req.getParameterValues("num");
		String[] unitPrice = req.getParameterValues("unitPrice");
		List<Poitem> poitems = new ArrayList<Poitem>();
		for(int i=0;i<productCodes.length;i++) {//遍历生成poitem对象集合
			Poitem poitem = new Poitem();
			poitem.setPoId(Long.parseLong(poId));
			poitem.setProductCode(productCodes[i]);
			poitem.setUnitName(unitName[i]);
			poitem.setUnitPrice(Double.parseDouble(unitPrice[i]));
			poitem.setNum(Integer.parseInt(num[i]));
			poitem.setItemPrice(Double.parseDouble(unitPrice[i])*Integer.parseInt(num[i]));
			poitems.add(poitem);
		}
		//生成pomain对象
		Pomain pomain = new Pomain();
		pomain.setPoId(Long.parseLong(poId));
		pomain.setCreateTime(createTime);
		pomain.setVenderCode(venderCode);
		pomain.setAccount(account);
		pomain.setTipFee(Double.parseDouble(tipFee));
		pomain.setPoTotal(Double.parseDouble(productTotal)+Double.parseDouble(tipFee));
		pomain.setProductTotal(Double.parseDouble(productTotal));
		pomain.setPayType(Integer.parseInt(payType));
		pomain.setRemark(remark);
		pomain.setPrePayFee(Double.parseDouble(prePayFee));
		pomain.setStatus(1);
		Connection conn = null;//事务处理
		try {
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			int i = new PomainDao(conn).add(pomain);
			int[] arr = new PoitemDao(conn).add(poitems); 
			for(int n=0;n<poitems.size();n++) {//修改产品表在途数
				String productCode = poitems.get(n).getProductCode();
				int number = poitems.get(n).getNum();
				int addnum = new ProductDao(conn).changePONum(productCode, number);
				if(addnum == 0){
					conn.rollback();//回滚事务
					return;
				}
			}
			if(i == 0){
				conn.rollback();//回滚事务
				return;
			}
			for(int j:arr){
				if(j == 0){
					conn.rollback();
					return;
				}
			}
			conn.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.close(conn, null, null);
			req.getRequestDispatcher("/main/pomain/ListNewPomain").forward(req, resp);
		}
	}

}
